[[...path]].page.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import React, { useEffect } from 'react';
  2. import { pagePathUtils } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import Head from 'next/head';
  7. import { useRouter } from 'next/router';
  8. // import { PageAlerts } from '~/components/PageAlert/PageAlerts';
  9. // import { PageComments } from '~/components/PageComment/PageComments';
  10. // import { useTranslation } from '~/i18n';
  11. import { CrowiRequest } from '~/interfaces/crowi-request';
  12. // import { renderScriptTagByName, renderHighlightJsStyleTag } from '~/service/cdn-resources-loader';
  13. // import { useIndentSize } from '~/stores/editor';
  14. // import { useRendererSettings } from '~/stores/renderer';
  15. // import { EditorMode, useEditorMode, useIsMobile } from '~/stores/ui';
  16. import { IPageWithMeta } from '~/interfaces/page';
  17. import { serializeUserSecurely } from '~/server/models/serializers/user-serializer';
  18. import { useSWRxCurrentPage, useSWRxPageInfo } from '~/stores/page';
  19. import loggerFactory from '~/utils/logger';
  20. // import { isUserPage, isTrashPage, isSharedPage } from '~/utils/path-utils';
  21. // import GrowiSubNavigation from '../client/js/components/Navbar/GrowiSubNavigation';
  22. // import GrowiSubNavigationSwitcher from '../client/js/components/Navbar/GrowiSubNavigationSwitcher';
  23. // import DisplaySwitcher from '../client/js/components/Page/DisplaySwitcher';
  24. import { BasicLayout } from '../components/BasicLayout';
  25. // import { serializeUserSecurely } from '../server/models/serializers/user-serializer';
  26. // import PageStatusAlert from '../client/js/components/PageStatusAlert';
  27. import {
  28. useCurrentUser, useCurrentPagePath,
  29. useOwnerOfCurrentPage,
  30. useIsForbidden, useIsNotFound, useIsTrashPage, useShared, useShareLinkId, useIsSharedUser, useIsAbleToDeleteCompletely,
  31. useAppTitle, useSiteUrl, useConfidential, useIsEnabledStaleNotification,
  32. useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsMailerSetup,
  33. useAclEnabled, useHasSlackConfig, useDrawioUri, useHackmdUri, useMathJax, useNoCdn, useEditorConfig, useCsrfToken,
  34. } from '../stores/context';
  35. import { CommonProps, getServerSideCommonProps, useCustomTitle } from './commons';
  36. import { PageModel } from '~/server/models/page';
  37. import { isValidObjectId } from 'mongoose';
  38. // import { useCurrentPageSWR } from '../stores/page';
  39. const logger = loggerFactory('growi:pages:all');
  40. const { isUsersHomePage, isTrashPage: _isTrashPage } = pagePathUtils;
  41. type Props = CommonProps & {
  42. currentUser: string,
  43. pageWithMetaStr: string,
  44. // pageUser?: any,
  45. // redirectTo?: string;
  46. // redirectFrom?: string;
  47. // shareLinkId?: string;
  48. isForbidden: boolean,
  49. isNotFound: boolean,
  50. // isAbleToDeleteCompletely: boolean,
  51. isSearchServiceConfigured: boolean,
  52. isSearchServiceReachable: boolean,
  53. // isMailerSetup: boolean,
  54. // isAclEnabled: boolean,
  55. // hasSlackConfig: boolean,
  56. // drawioUri: string,
  57. // hackmdUri: string,
  58. // mathJax: string,
  59. // noCdn: string,
  60. // highlightJsStyle: string,
  61. // isAllReplyShown: boolean,
  62. // isContainerFluid: boolean,
  63. // editorConfig: any,
  64. // isEnabledStaleNotification: boolean,
  65. // isEnabledLinebreaks: boolean,
  66. // isEnabledLinebreaksInComments: boolean,
  67. // adminPreferredIndentSize: number,
  68. // isIndentSizeForced: boolean,
  69. };
  70. const GrowiPage: NextPage<Props> = (props: Props) => {
  71. // const { t } = useTranslation();
  72. const router = useRouter();
  73. const { data: currentUser } = useCurrentUser(props.currentUser != null ? JSON.parse(props.currentUser) : null);
  74. // commons
  75. useAppTitle(props.appTitle);
  76. useSiteUrl(props.siteUrl);
  77. // useEditorConfig(props.editorConfig);
  78. useConfidential(props.confidential);
  79. useCsrfToken(props.csrfToken);
  80. // page
  81. useCurrentPagePath(props.currentPathname);
  82. // useOwnerOfCurrentPage(props.pageUser != null ? JSON.parse(props.pageUser) : null);
  83. // useIsForbidden(props.isForbidden);
  84. // useNotFound(props.isNotFound);
  85. // useIsTrashPage(_isTrashPage(props.currentPagePath));
  86. // useShared(isSharedPage(props.currentPagePath));
  87. // useShareLinkId(props.shareLinkId);
  88. // useIsAbleToDeleteCompletely(props.isAbleToDeleteCompletely);
  89. // useIsSharedUser(props.currentUser == null && isSharedPage(props.currentPagePath));
  90. // useIsEnabledStaleNotification(props.isEnabledStaleNotification);
  91. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  92. useIsSearchServiceReachable(props.isSearchServiceReachable);
  93. // useIsMailerSetup(props.isMailerSetup);
  94. // useAclEnabled(props.isAclEnabled);
  95. // useHasSlackConfig(props.hasSlackConfig);
  96. // useDrawioUri(props.drawioUri);
  97. // useHackmdUri(props.hackmdUri);
  98. // useMathJax(props.mathJax);
  99. // useNoCdn(props.noCdn);
  100. // useIndentSize(props.adminPreferredIndentSize);
  101. // useRendererSettings({
  102. // isEnabledLinebreaks: props.isEnabledLinebreaks,
  103. // isEnabledLinebreaksInComments: props.isEnabledLinebreaksInComments,
  104. // adminPreferredIndentSize: props.adminPreferredIndentSize,
  105. // isIndentSizeForced: props.isIndentSizeForced,
  106. // });
  107. // const { data: editorMode } = useEditorMode();
  108. let pageWithMeta: IPageWithMeta | undefined;
  109. if (props.pageWithMetaStr != null) {
  110. pageWithMeta = JSON.parse(props.pageWithMetaStr) as IPageWithMeta;
  111. }
  112. useSWRxCurrentPage(undefined, pageWithMeta?.data); // store initial data
  113. useSWRxPageInfo(pageWithMeta?.data._id, undefined, pageWithMeta?.meta); // store initial data
  114. const classNames: string[] = [];
  115. // switch (editorMode) {
  116. // case EditorMode.Editor:
  117. // classNames.push('on-edit', 'builtin-editor');
  118. // break;
  119. // case EditorMode.HackMD:
  120. // classNames.push('on-edit', 'hackmd');
  121. // break;
  122. // }
  123. // if (props.isContainerFluid) {
  124. // classNames.push('growi-layout-fluid');
  125. // }
  126. // if (page == null) {
  127. // classNames.push('not-found-page');
  128. // }
  129. // // Rewrite browser url by Shallow Routing https://nextjs.org/docs/routing/shallow-routing
  130. // useEffect(() => {
  131. // if (props.redirectTo != null) {
  132. // router.push('/[[...path]]', props.redirectTo, { shallow: true });
  133. // }
  134. // // eslint-disable-next-line react-hooks/exhaustive-deps
  135. // }, []);
  136. return (
  137. <>
  138. <Head>
  139. {/*
  140. {renderScriptTagByName('drawio-viewer')}
  141. {renderScriptTagByName('mathjax')}
  142. {renderScriptTagByName('highlight-addons')}
  143. {renderHighlightJsStyleTag(props.highlightJsStyle)}
  144. */}
  145. </Head>
  146. {/* <BasicLayout title={useCustomTitle(props, t('GROWI'))} className={classNames.join(' ')}> */}
  147. <BasicLayout title={useCustomTitle(props, 'GROWI')} className={classNames.join(' ')}>
  148. <header className="py-0">
  149. {/* <GrowiSubNavigation /> */}
  150. GrowiSubNavigation
  151. </header>
  152. <div className="d-edit-none">
  153. {/* <GrowiSubNavigationSwitcher /> */}
  154. GrowiSubNavigationSwitcher
  155. </div>
  156. <div id="grw-subnav-sticky-trigger" className="sticky-top"></div>
  157. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  158. <div id="main" className={`main ${isUsersHomePage(props.currentPathname) && 'user-page'}`}>
  159. <div className="row">
  160. <div className="col grw-page-content-container">
  161. <div id="content-main" className="content-main grw-container-convertible">
  162. {/* <PageAlerts /> */}
  163. PageAlerts<br />
  164. {/* <DisplaySwitcher /> */}
  165. DisplaySwitcher<br />
  166. <div id="page-editor-navbar-bottom-container" className="d-none d-edit-block"></div>
  167. {/* <PageStatusAlert /> */}
  168. PageStatusAlert
  169. </div>
  170. </div>
  171. {/* <div className="col-xl-2 col-lg-3 d-none d-lg-block revision-toc-container">
  172. <div id="revision-toc" className="revision-toc mt-3 sps sps--abv" data-sps-offset="123">
  173. <div id="revision-toc-content" className="revision-toc-content"></div>
  174. </div>
  175. </div> */}
  176. </div>
  177. </div>
  178. <footer>
  179. {/* <PageComments /> */}
  180. PageComments
  181. </footer>
  182. </BasicLayout>
  183. </>
  184. );
  185. };
  186. async function injectPageInformation(context: GetServerSidePropsContext, props: Props): Promise<void> {
  187. const req: CrowiRequest = context.req as CrowiRequest;
  188. const { crowi } = req;
  189. const Page = crowi.model('Page');
  190. const { pageService } = crowi;
  191. const { user } = req;
  192. const { currentPathname } = props;
  193. // determine pageId
  194. let pageId;
  195. const pageIdStr = currentPathname.substring(1);
  196. pageId = isValidObjectId(pageIdStr) ? pageIdStr : null;
  197. const result: IPageWithMeta = await pageService.findPageAndMetaDataByViewer(pageId, currentPathname, user, true); // includeEmpty = true, isSharedPage = false
  198. const page = result.data;
  199. if (page == null) {
  200. const count = pageId != null ? await Page.count({ _id: pageId }) : await Page.count({ path: currentPathname }) ;
  201. // check the page is forbidden or just does not exist.
  202. props.isForbidden = count > 0;
  203. props.isNotFound = true;
  204. logger.warn(`Page is ${props.isForbidden ? 'forbidden' : 'not found'}`, currentPathname);
  205. }
  206. await (page as unknown as PageModel).populateDataToShowRevision();
  207. props.pageWithMetaStr = JSON.stringify(result);
  208. }
  209. // async function injectPageUserInformation(context: GetServerSidePropsContext, props: Props): Promise<void> {
  210. // const req: CrowiRequest = context.req as CrowiRequest;
  211. // const { crowi } = req;
  212. // const UserModel = crowi.model('User');
  213. // if (isUserPage(props.currentPagePath)) {
  214. // const user = await UserModel.findUserByUsername(UserModel.getUsernameByPath(props.currentPagePath));
  215. // if (user != null) {
  216. // props.pageUser = JSON.stringify(user.toObject());
  217. // }
  218. // }
  219. // }
  220. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  221. const req: CrowiRequest = context.req as CrowiRequest;
  222. const { crowi } = req;
  223. const {
  224. appService, searchService, configManager, aclService, slackNotificationService, mailService,
  225. } = crowi;
  226. const { user } = req;
  227. const result = await getServerSideCommonProps(context);
  228. // check for presence
  229. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  230. if (!('props' in result)) {
  231. throw new Error('invalid getSSP result');
  232. }
  233. const props: Props = result.props as Props;
  234. await injectPageInformation(context, props);
  235. if (user != null) {
  236. props.currentUser = JSON.stringify(user);
  237. }
  238. props.isSearchServiceConfigured = searchService.isConfigured;
  239. props.isSearchServiceReachable = searchService.isReachable;
  240. // props.isMailerSetup = mailService.isMailerSetup;
  241. // props.isAclEnabled = aclService.isAclEnabled();
  242. // props.hasSlackConfig = slackNotificationService.hasSlackConfig();
  243. // props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  244. // props.hackmdUri = configManager.getConfig('crowi', 'app:hackmdUri');
  245. // props.mathJax = configManager.getConfig('crowi', 'app:mathJax');
  246. // props.noCdn = configManager.getConfig('crowi', 'app:noCdn');
  247. // props.highlightJsStyle = configManager.getConfig('crowi', 'customize:highlightJsStyle');
  248. // props.isAllReplyShown = configManager.getConfig('crowi', 'customize:isAllReplyShown');
  249. // props.isContainerFluid = configManager.getConfig('crowi', 'customize:isContainerFluid');
  250. // props.isEnabledStaleNotification = configManager.getConfig('crowi', 'customize:isEnabledStaleNotification');
  251. // props.isEnabledLinebreaks = configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks');
  252. // props.isEnabledLinebreaksInComments = configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments');
  253. // props.editorConfig = {
  254. // upload: {
  255. // image: crowi.fileUploadService.getIsUploadable(),
  256. // file: crowi.fileUploadService.getFileUploadEnabled(),
  257. // },
  258. // };
  259. // props.adminPreferredIndentSize = configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize');
  260. // props.isIndentSizeForced = configManager.getConfig('markdown', 'markdown:isIndentSizeForced');
  261. return {
  262. props,
  263. };
  264. };
  265. export default GrowiPage;